package logic

import (
	"context"
	"errors"
	"srv/internal/svc"
	"srv/models/mysql"
	"srv/models/redisInit"
	"srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsAddLogic {
	return &GoodsAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsAddLogic) GoodsAdd(in *shop.AddGoodsRequest) (*shop.AddGoodsResponse, error) {
	// todo: add your logic here and delete this line
	g := mysql.Goods{}
	err := g.CheckGoods(int(in.GoodsId))
	if err != nil {
		logx.Error("商品不存在")
		return nil, err
	}
	if g.ID == 0 {
		logx.Error("商品不存在")
		return nil, err
	}

	if g.GoodsNum < int(in.Num) {
		logx.Error("库存不足")
		return nil, errors.New("库存不足")
	}
	redisInit.AddGoods(int(in.UserId), int(in.GoodsId), int(in.Num), g.GoodsName, g.GoodsState, g.GoodsPrice)
	return &shop.AddGoodsResponse{}, nil
}
